package org.kisekiproject.evaluator.nodes {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import org.kisekiproject.evaluator.IVariableNamespace;

	/**
	 * Method call.
	 */
	public class MethodNode extends EventDispatcher implements ISyntaxNode {

		private var _index:int;
		private var _name:String;
		private var _arguments:Array;
		private var _variables:IVariableNamespace;
		private var _objectNode:ISyntaxNode;

		/**
		 * Constructor.
		 */
		public function MethodNode(variables:IVariableNamespace, object:ISyntaxNode, name:String, arguments:Array, index:int) {
			_index=index;
			_name=name;
			_arguments=arguments;
			_variables=variables;
			_objectNode=object;

			for each (var arg:ISyntaxNode in _arguments)
				arg.addEventListener(Event.CHANGE,onDependencyChange);

			_objectNode.addEventListener(Event.CHANGE,onDependencyChange);
		}

		/**
		 * Dependency change.
		 */
		private function onDependencyChange(e:Event):void {
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		/**
		 * Get value.
		 */
		public function get value():Object {
			var argValues:Array=new Array();

			for each (var arg:ISyntaxNode in _arguments)
				argValues.push(arg.value);

			/*var o:Object=_objectNode.value;

			trace("getting value from method call, arg values: "+argValues+" method: "+_name+" object: "+o);*/

			return _objectNode.value[_name].apply(null,argValues);
		}

		/**
		 * Get stream index.
		 */
		public function get index():int {
			return _index;
		}
	}
}